#!/usr/bin/perl -w

use strict;
use warnings;

my $FILTERTYPE = 'csv';

my $SEP = ',';
my $NL = "\n";

if ($#ARGV < 1) {
    die "Filter failed! Please report bug.\n";
}

my $filename = $ARGV[0];
my $fileType  = $ARGV[1];
my $infile = $filename;

open INFILE,"< $filename";
$filename =~ s/\.tmp/\.$FILTERTYPE/;
open OUTFILE,"> $filename";

if ($fileType eq 'topology') {
    my $region = 'topo';
    print OUTFILE 'THREADS'.$NL;

    while (<INFILE>) {

        if (/Cache Topology/) {
            $region = 'cache';
            print OUTFILE 'CACHES'.$NL;
        } elsif (/NUMA Topology/) {
            $region = 'numa';
            print OUTFILE 'NUMA'.$NL;
        }

        if ($region eq 'topo') {
            if (/(CPU type):\t(.*)/) {
                print OUTFILE $1.$SEP.$2.$NL;
            }
            elsif (/([A-Za-z ]*):\t([0-9]*)/) {
                print OUTFILE $1.$SEP.$2.$NL;
            } elsif (/(HWThread)\t(Thread)\t\t(Core)\t\t(Socket)/) {
                print OUTFILE $1.$SEP.$2.$SEP.$3.$SEP.$4.$NL;
            } elsif (/([0-9]*)\t\t([0-9]*)\t\t([0-9]*)\t\t([0-9]*)/) {
                print OUTFILE $1.$SEP.$2.$SEP.$3.$SEP.$4.$NL;
            }
        } elsif ($region eq 'cache') {
            if (/(Size):\t([0-9]*) ([kMB]*)/) {
                my $size = $2;
                if ($3 eq 'MB') {
                    $size *= 1024;
                }
                print OUTFILE $1.'[kB]'.$SEP.$size.$NL;
            } elsif (/(Cache groups):\t*(.*)/) {
                my @groups = split('\) \(',$2);

                my $grpId = 0;
                foreach (@groups) {
                    /([0-9 ]+)/;
                    print OUTFILE 'Cache group '.$grpId.$SEP.$1.$NL;
                    $grpId++;
                }
            } elsif (/(.*):\t*(.*)/) {
                print OUTFILE $1.$SEP.$2.$NL;
            }
        } elsif ($region eq 'numa') {
            if (/Domain ([0-9]*)/) {
                print OUTFILE 'Domain ID'.$SEP.$1.$NL;
            } elsif (/Memory:.*total ([0-9.]+) MB/) {
                print OUTFILE 'Memory [MB]'.$SEP.$1.$NL;
            } elsif (/(.*):\t*[ ]*(.*)/) {
                print OUTFILE $1.$SEP.$2.$NL;
            }
        }
    }
} elsif ($fileType eq 'perfctr') {
    my $header = 0;
    while (<INFILE>) {
        if (/Event[ ]*\|[ ]*(core.*)\|/) {
            if (not $header) {
                my @col = split('\|',$1);
                my $numcol = $#col+1;
                print OUTFILE 'NumColumns'.$SEP.$numcol.$NL;
                print OUTFILE 'Event/Metric';
                foreach (@col) {
                    s/[ ]//g;
                    print OUTFILE $SEP.$_;
                }
                print OUTFILE $NL;
                $header = 1;
            }
        }elsif (/STAT/) {

        }elsif (/\|[ ]+([A-Z0-9_]+)[ ]+\|[ ]*(.*)\|/) {
            my @col = split('\|',$2);
            print OUTFILE $1;
            foreach (@col) {
                s/[ ]//g;
                print OUTFILE $SEP.$_;
            }
            print OUTFILE $NL;
        } 
    }
} else {
    die "Filter failed! Unknown application type $fileType!\n";
}

unlink($infile);
close INFILE;
close OUTFILE;


